package com.woyou.model.dao;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EBean.Scope;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

/**
 * 封装搜索关键字的数据操作
 * @author longtao.li
 *
 */
@EBean(scope=Scope.Singleton)
public class SearchKeyDao {

	private static final String TAG = "SearchKeyDao";
	
	public static final String PREFERNCE_FILE_NAME = "serch_key"; //缓存文件名

	/**
	 * 获取搜索关键字历史记录
	 * @param context
	 * @return
	 */
	public List<String>  getKeys(Context context){
		List<String> tmp = new ArrayList<String>();
		
		if(context == null){
			Log.e(TAG, "context null");
			return tmp;
		}
		SharedPreferences prefe = context.getSharedPreferences(PREFERNCE_FILE_NAME, 0);
		for(int i=0; i<10; i++){
			tmp.add(prefe.getString("keys"+i,""));
		}
		
		return tmp;
	}
	
	/**
	 * 保存外卖搜索关键字
	 * @param context
	 * @param keys
	 */
	public void putKeys(Context context, List<String> keys){
		if(context == null || keys == null){
			Log.e(TAG, "context null");
			return;
		}
		SharedPreferences prefe = context.getSharedPreferences(PREFERNCE_FILE_NAME, 0);
		Editor editor = prefe.edit();
		for(int i=0; i<10; i++){
			if(i>=keys.size()){
				editor.putString("keys" + i, "");
			}else{
				editor.putString("keys" + i, (String)keys.get(i));
			}
		}
		editor.commit();
	}
	
	/**
	 * 清空外卖搜索关键字
	 * @param context
	 */
	public void clearKeys(Context context){
		if( context == null ){
			Log.e(TAG, "context null");
			return;
		}
		SharedPreferences prefe = context.getSharedPreferences(PREFERNCE_FILE_NAME, 0);
		Editor editor = prefe.edit();
		editor.clear();
		editor.commit();
	}
	
}
